// https://vuex.vuejs.org/zh-cn/intro.html
// make sure to call Vue.use(Vuex) if using a module system
import Vue from 'vue'
import Vuex from 'vuex'
import '@/utils/index'
import {isEmpty} from 'lodash'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    appid: '',
    userId: '',
    authCode: '',
    userInfo: {},
    companyId: '',
    companyName: '',
    buyer: false,
    dataUrl: 'http://localhost:9090',
    // server: 'http://localhost:9090'
    server: 'https://mb.ynhotel.net'
    // server: 'http://47.108.177.23:8080'
  },
  mutations: {
    setCompanyId: (state, companyId) => {
      const obj = state
      obj.companyId = companyId
    },
    setBuyer: (state, flag) => {
      const obj = state
      obj.buyer = flag
    },
    setCompanyName: (state, companyName) => {
      const obj = state
      obj.companyName = companyName
    },
    clearCompanyName (state) {
      const obj = state
      obj.companyName = ''
      obj.companyId = ''
    },
    setAppid: (state, appid) => {
      const obj = state
      obj.appid = appid
    },
    setAuthCode: (state, authCode) => {
      const obj = state
      obj.authCode = authCode
      if (!isEmpty(obj.authCode)) {
        // 该url是您自己的服务地址，实现的功能是服务端拿到authcode去开放平台进行token验证
        let url = obj.server + '/auth/authUser'
        uni.request({
          url: url,
          data: {
            authCode: authCode
          },
          success: (res) => {
            const {data} = res
            obj.userId = data.userId
            // this.mpvue.alert({
            //   content: '你的支付宝唯一ID：' + data.userId
            // })
          },
          fail: (error) => {
            this.mpvue.alert({
              content: error.errorMessage
            })
          }
        })
      }
    },
    setAuthCodeNoId: (state, authCode) => {
      const obj = state
      obj.authCode = authCode
    },
    setUserInfo: (state, userinfo) => {
      const obj = state
      obj.userInfo = userinfo
    }
  }
})

export default store
